#coding=utf-8
__author__ = 'liubin'

from project.tools import Base
from sqlalchemy import Column, DateTime, Integer, SmallInteger, String, BigInteger, ForeignKey, Boolean, Text, Numeric
from sqlalchemy.orm import relationship, backref
from sqlalchemy.sql.expression import text
from project.tools import JsonpickleMixin
from base import BaseModel

class SalesOrder(BaseModel, Base):
    """
    销售订单表
    """
    __tablename__ = 't_sales_order'
    no = Column('no', String(50), nullable=False, doc=u'订单号')
    orderDate = Column('order_date', DateTime, server_default=text('NOW()'), doc=u'收货日期')
    customerId = Column('customer_id', BigInteger, ForeignKey('m_customer.id'), nullable=False, doc=u'客户ID')
    customer = relationship("Customer", foreign_keys=[customerId])
    amount = Column('amount', Numeric, nullable=False, default=0, server_default='0', doc=u'金额')

    # # 种类名
    # name = Column('name', String(200), nullable=False, doc=u'名称')
    # # 备注
    comment = Column('comment', Text, doc=u'备注')
    # 是否被删除
    #isDeleted = Column('is_deleted', Boolean, server_default=text('0'), doc=u'是否已删除')
    status = Column('status', SmallInteger, nullable=False, default=0, server_default='0', doc=u'状态')

    def __init__(self, data=None):
        """
        初始化
        :param data:
        :return:
        """
        if data:
            self.no = data.get('no')
            self.customerId = data.get('customerId')
            self.orderDate = data.get('orderDate')
            self.comment = data.get('comment')

class SalesOrderDetail(BaseModel, Base):
    """
    销售订单明细表
    """
    __tablename__ = 't_sales_order_detail'
    orderId = Column('order_id', BigInteger, ForeignKey('t_sales_order.id'), nullable=False, doc=u'订单ID')
    order = relationship("SalesOrder", backref="detail", foreign_keys=[orderId])
    categoryId = Column('category_id', BigInteger, ForeignKey('m_category.id'), nullable=False, doc=u'布料种类ID')
    category = relationship("Category", foreign_keys=[categoryId])
    colorId = Column('color_id', BigInteger, ForeignKey('m_color.id'), nullable=False, doc=u'颜色ID')
    color = relationship("Color", foreign_keys=[colorId])
    pileHeight = Column('pile_height', Numeric, nullable=False, default=0, server_default='0', doc=u'毛高')
    fabricWidth = Column('fabric_width', Numeric, nullable=False, default=0, server_default='0', doc=u'门幅')
    price = Column('price', Numeric, nullable=False, default=0, server_default='0', doc=u'单价')
    count = Column('count', Numeric, nullable=False, default=0, server_default='0', doc=u'数量')
    amount = Column('amount', Numeric, nullable=False, default=0, server_default='0', doc=u'金额')
    comment = Column('comment', Text, doc=u'备注')
    status = Column('status', SmallInteger, nullable=False, default=0, server_default='0', doc=u'状态')
    #isDeleted = Column('is_deleted', Boolean, server_default=text('0'), doc=u'是否已删除')
    def __init__(self, data=None):
        """
        初始化
        :param data:
        :return:
        """
        if data:
            self.orderId = data.get('orderId')
            self.categoryId = data.get('categoryId')
            self.colorId = data.get('colorId')
            self.pileHeight = data.get('pileHeight')
            self.fabricWidth = data.get('fabricWidth')
            self.price = data.get('price')
            self.count = data.get('count')
            self.comment = data.get('comment')
            self.status = data.get('status')